<?php
/**
 * Filters date. If date is valid - returns this date in output format,
 * otherwise returns current date in output format.
 *
 * @author den
 */
class Lemar_Filter_DefaultDate implements Zend_Filter_Interface
{
	/**
	 * Input format
	 *
	 * @var string
	 */
	protected $_sFormatIn;

	/**
	 * Output format
	 *
	 * @var string
	 */
	protected $_sFormatOut;

	public function __construct($sFormatIn = Lemar_Date::SITE_DATE, $sFormatOut = Lemar_Date::MYSQL_DATE)
	{
		$this->setFormatIn($sFormatIn);
		$this->setFormatOut($sFormatOut);
	}

	public function setFormatIn($sFormatIn)
	{
		$this->_sFormatIn = (string) $sFormatIn;
		return $this;
	}

	public function getFormatIn()
	{
		return $this->_sFormatIn;
	}

	public function setFormatOut($sFormatOut)
	{
		$this->_sFormatOut = (string) $sFormatOut;
		return $this;
	}

	public function getFormatOut()
	{
		return $this->_sFormatOut;
	}

	public function filter($sValue)
	{
		$oZendDateValidator = new Zend_Validate_Date($this->getFormatIn());
		return
			$oZendDateValidator->isValid($sValue)
			?
			Lemar_Date::convertFormats($sValue, $this->getFormatIn(), $this->getFormatOut())
			:
			Lemar_Date::getCurrentDate($this->getFormatOut());
	}
}